#include <stdlib.h>
#include "hashTable.h"

/************************************************
 * main
 ************************************************/

int x = 0;
int y = 1;
int z = 2;
int w = 194;
int *k1,*k2,*k3,*v1,*v2,*v3;



int
hashtable_hash_functon(void* k){
  return *(int *)k;
}

int hashtable_key_comp_func(void* keyA, void* keyB){
	if (*(int *)keyA==*(int *)keyB) return 0;
	return 1;
}
void 
  test_hashtable(){
  

  printf("TEST HASHTABLE\n");
  printf("CREATE AND ADD\n");
  v1  = (int *) malloc(sizeof(int));
  v2   = (int *) malloc(sizeof(int));
  v3   = (int *) malloc(sizeof(int));
  k1   = (int *) malloc(sizeof(int));
  k2   = (int *) malloc(sizeof(int));
  k3   = (int *) malloc(sizeof(int));
  *k1 = 1;
  *k2 = 2;
  *k3 = 1;
  *v1 = 5;
  *v2 = 6; 
  *v3 = 7; 
  hashTable_t h = hashTable_create(100, hashtable_hash_functon, hashtable_key_comp_func);
  addHashItem(h, k1, v1, free, free);
  addHashItem(h, k2, v2, free, free);
  hashtable_debug(h);
  
  printf("TEST DUPLICATE INSERTION\n");
  addHashItem(h, k3, v3, free, free);
  printf("ITEMS WITH THE SAME KEY AS NEW KEY ARE BEING REPLACED\n");
	//free(k3);
	//free(v3);
	k3 = (int*) malloc(sizeof(int));
	v3 = (int*) malloc(sizeof(int));
	*k3 = 1;
	*v3 = 1;
	addHashItem(h, k3, v3, free, free);
	printf("TEST SAME KEY REMALLOCED\n");
  hashtable_debug(h);

  printf("TEST LOOK UP KEY = 1 ");
  int* v ;
  int v_int; 
  if (lookupHashItem(h, k3)==NULL){
    printf("Not found\n");
  }else{
    v = lookupHashItem(h, k3);
    v_int = *(int *)v;
    printf("found [%d, %d]\n", *k3, v_int);
  }

  printf("TEST LOOK UP KEY = 2 ");
  if (lookupHashItem(h, k2)==NULL){
    printf("Not found\n");
  }else{
    v = lookupHashItem(h, k2);
    v_int = *(int *)v;
    printf("found [%d, %d]\n", *k2, v_int);
  }
  printf("TEST DESTROY\n");
  hashTable_destroy(h, free, free);
  printf("DONE\n");
  return;
}

int main(int argc, char* argv[]) {
	test_hashtable();	
  	return 0;
}
